﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace area_caculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            textBox2.Text = null;
            textBox3.Text = null;
            textBox4.Text = null;
            textBox5.Text = null;

            textBox2.Enabled = true;
            textBox3.Enabled = true;
            textBox4.Enabled = true;
            textBox5.Enabled = true;
        }


       




        public abstract class Shape
        {
            public abstract double Area();
        }

        public class Square : Shape
        {
            double side;
            public Square(double side)
            {
                this.side = side;
            }
            public override double Area()
            {
                return side * side;
            }
        }

        public class Rect : Shape
        {
            double length;
            double width;
            public Rect(double length, double width)
            {
                this.length = length;
                this.width = width;
            }
            public override double Area()
            {
                return length*width;
            }
        }

        public class Triangle : Shape
        {
            double s1;
            double s2;
            double s3;
            public Triangle(double s1, double s2,double s3)
            {
                this.s1 = s1;
                this.s2 = s2;
                this.s3 = s3;
            }

            public override double Area()
            {
                //利用海伦公式计算三角形面积
                double trianglearea;
                double p;
                p = (s1 + s2 + s3) / 2;
                trianglearea = System.Math.Sqrt(p * (p - s1) * (p - s2) * (p - s3));

                return trianglearea;
            }

        }



        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
            {
                MessageBox.Show("这不是一个正方形！");
            }

            double sidea = double.Parse(textBox2.Text);
            double sideb = double.Parse(textBox3.Text);
            double sidec = double.Parse(textBox4.Text);
            double sided = double.Parse(textBox5.Text);

            //派生类的实例化
            JSquare jSquare = new JSquare();
            Square square = new Square(sidea);

            //首先进行形状的判断，然后符合要求的计算面积
            if(!jSquare.judgeShape(sidea, sideb,sidec,sided))
            {
                MessageBox.Show("边长不符合正方形标准！");
            }
            else
            {
                double area=square.Area();
                string s = Convert.ToString(area);
                textBox1.Text = s;//在TextBox里输出面积结果
            }



        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
            {
                MessageBox.Show("这不是一个长方形！");
            }

            double sidea = double.Parse(textBox2.Text);
            double sideb = double.Parse(textBox3.Text);
            double sidec = double.Parse(textBox4.Text);
            double sided = double.Parse(textBox5.Text);

            SortMax(ref sidea, ref sideb, ref sidec, ref sided);

            JRect jRect = new JRect();
            Rect rect = new Rect(sidea, sided);

            if (!jRect.judgeShape(sidea, sideb, sidec, sided))
            {
                MessageBox.Show("边长不符合正方形标准！");
            }
            else
            {
                double area = rect.Area();
                string s = Convert.ToString(area);
                textBox1.Text = s;//在TextBox里输出面积结果
            }


        }

        private void button3_Click(object sender, EventArgs e)
        {
            double sidea = double.Parse(textBox2.Text);
            double sideb = double.Parse(textBox3.Text);
            double sidec = double.Parse(textBox4.Text);
            double sided = double.Parse(textBox5.Text);

            SortMax(ref sidea, ref sideb, ref sidec, ref sided);

            double cha = sided * sided - sideb * sideb - sidec * sidec;

            if(radioButton1.Checked&&cha!=0)
            {
                MessageBox.Show("这不符合勾股定理！");
            }

            JTriangle jTriangle = new JTriangle();
            Triangle triangle = new Triangle(sideb,sidec,sided);

            if (!jTriangle.judgeShape(sidea, sideb, sidec, sided))
            {
                MessageBox.Show("边长不符合三角形标准！");
            }
            else
            {
                double area = triangle.Area();
                string s = Convert.ToString(area);
                textBox1.Text = s;

            }
        }

        



        private abstract class JudgeShape
        {
            public abstract bool judgeShape(double a, double b, double c,double d);
        }

        private class JSquare:JudgeShape
        {
            public override bool judgeShape(double a,double b, double c,double d)
            {
                if(a!=b||a!=c||a!=d)
                {
                    return false;
                }

                else return true;
            }
        }

        private class JRect:JudgeShape
        {
            public override bool judgeShape(double a, double b, double c, double d)
            {
                SortMax(ref a, ref b, ref c, ref d);

                if (a != b || c != d||a==0||c==0)
                {
                    return false;
                }
                else return true;
            }
        }

        private class JTriangle:JudgeShape
        {
            
            public override bool judgeShape(double a, double b, double c, double d)
            {
                
                SortMax(ref a,ref b, ref c,ref d);

                //非三角形返回false
                if (a != 0) return false;
                else
                {
                    //两边之和大于第三边
                    if (b+c<=d)
                    {
                        return false;
                    }    
                }
                return true;
            }
        }

        //排序函数，用于三角形和长方形的判断
        public static void SortMax(ref double a,ref double b,ref double c, ref double d)
        {
            double t;
            if (a > b)
            {
                t = a; a = b; b = t;
            }
            if (a > c)
            {
                t = a; a = c; c = t;
            }
            if (a > d)
            {
                t = a; a = d; d = t;
            }
            if (b > d)
            {
                t = b; b = d; d = t;
            }
            if (b > c)
            {
                t = b; b = c; c = t;
            }
            if (c > d)
            {
                t = c; c = d; d = t;
            }
        }

       
    }
}
